package com.macro.mall.portal.service.impl;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.macro.mall.mapper.BmsSightseeingBusApplicantMapper;
import com.macro.mall.mapper.BmsSightseeingBusPointMapper;
import com.macro.mall.model.BmsSightseeingBusApplicant;
import com.macro.mall.model.BmsSightseeingBusPoint;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.dto.BmsSightseeingBusApplicantParam;
import com.macro.mall.portal.service.BmsSightseeingBusApplicantService;
import com.macro.mall.portal.service.OssService;
import com.macro.mall.portal.util.MemberUtil;
import com.macro.mall.portal.util.MemberUtils;
import com.macro.mall.portal.util.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Date;
import java.util.List;


/**
 * 观光车点位Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsSightseeingBusApplicantServiceImpl implements BmsSightseeingBusApplicantService {
    @Autowired
    private BmsSightseeingBusApplicantMapper bmsSightseeingBusApplicantMapper;
    @Autowired
    private UserUtils userUtils;

    @Autowired
    private BmsSightseeingBusPointMapper bmsSightseeingBusPointMapper;

    @Autowired
    private MemberUtil memberUtil;
    @Autowired
    private OssService ossService;

    /**
     * 分页查询观光车
     * @return
     */
    @Transactional
    @Override
    public List<BmsSightseeingBusApplicant> getList(BmsSightseeingBusApplicantParam bmsSightseeingBusApplicantParam) {
        BmsSightseeingBusApplicant bmsSightseeingBusApplicant = new BmsSightseeingBusApplicant();
        BeanUtils.copyProperties(bmsSightseeingBusApplicantParam, bmsSightseeingBusApplicant);
        //List<BmsSightseeingBusApplicant> list = bmsSightseeingBusApplicantMapper.selectAllXiao(bmsSightseeingBusApplicant);
        return null;
    }

    /**
     * 观光车报名
     * @param
     * @return
     */
    @Transactional
    @Override
    public BmsSightseeingBusApplicant create(BmsSightseeingBusApplicant bmsSightseeingBusApplicant) throws Exception {
        UmsMember umsMember= MemberUtils.getUser();
        //新增时，如果根据观光车id+userId查询数据库有数据，禁止预约乘车
        //获取tokenUseerID
        bmsSightseeingBusApplicant.setUserId(umsMember.getId());
        bmsSightseeingBusApplicant.setSightseeingBusId(bmsSightseeingBusApplicant.getSightseeingBusId());
        bmsSightseeingBusApplicant.setCreateTime(new Date());
        //如果大于0，则表示已经预约过，禁止预约
        Long count = bmsSightseeingBusApplicantMapper.selectApplicantPeople(bmsSightseeingBusApplicant);
        if (count > 0){
            throw new Exception("你已预约该辆观光车!");
        }
        //保存报名信息
        bmsSightseeingBusApplicant.setUserId(umsMember.getId());
        bmsSightseeingBusApplicant.setUserName(umsMember.getUsername());
        bmsSightseeingBusApplicant.setUserPhone(umsMember.getPhone());
        bmsSightseeingBusApplicant.setCreateUser(1L);
        bmsSightseeingBusApplicant.setUpdateUser(1L);
        bmsSightseeingBusApplicant.setCreateTime(new Date());
        bmsSightseeingBusApplicant.setUpdateTime(new Date());
        bmsSightseeingBusApplicant.setApplicationTime(bmsSightseeingBusApplicant.getApplicationTime());
        bmsSightseeingBusApplicant.setSightseeingBusPointId(bmsSightseeingBusApplicant.getSightseeingBusPointId());
        bmsSightseeingBusApplicant.setSightseeingBusId(bmsSightseeingBusApplicant.getSightseeingBusId());
        bmsSightseeingBusApplicant.setPeopleNumber(bmsSightseeingBusApplicant.getPeopleNumber());
        bmsSightseeingBusApplicant.setStatus(0);
        bmsSightseeingBusApplicant.setRemark(bmsSightseeingBusApplicant.getRemark());
        bmsSightseeingBusApplicant.setIsDelete(true);
        //生成二维码并保存
        String qrCode = generateQRCodeImage(String.valueOf(bmsSightseeingBusApplicant.getUserId()));
        //将二维码保存服务器中
        bmsSightseeingBusApplicant.setQrCode(qrCode);
        bmsSightseeingBusApplicantMapper.insert(bmsSightseeingBusApplicant);
        return bmsSightseeingBusApplicant;
    }

    //生成二维码
    public static String generateQRCodeImage(String text) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 200, 200);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        String base64Image = Base64.getEncoder().encodeToString(pngData);
        pngOutputStream.close();
        return base64Image;
    }


    /**
     * 取消预约
     * @param
     * @return
     */
    @Transactional
    @Override
    public int update(BmsSightseeingBusApplicant bmsSightseeingBusApplicant) {
        bmsSightseeingBusApplicant.setUpdateTime(new Date());
        bmsSightseeingBusApplicant.setStatus(3);
        return bmsSightseeingBusApplicantMapper.update(bmsSightseeingBusApplicant);
    }


    /**
     * 获取单个观光车点位信息
     * @param id
     * @return
     */
    @Override
    public BmsSightseeingBusApplicant getItem(Long id) {
        BmsSightseeingBusApplicant list = bmsSightseeingBusApplicantMapper.selectDetail(id);
        return list;

    }

    /**
     * 删除观光车点位
     * @param ids
     * @return
     * 完成
     */
    @Transactional
    @Override
    public int delete(List<Long> ids) {
        return bmsSightseeingBusApplicantMapper.delete(ids);

    }

    /**
     * 查询观光车上车点位
     * @return
     */
    @Override
    public List<BmsSightseeingBusPoint> getMpListPoint(BmsSightseeingBusPoint bmsSightseeingBusPoint) {
        return bmsSightseeingBusPointMapper.selectXiaoPoint(bmsSightseeingBusPoint);
    }


    /**
     * 二次进入查看个人报名成功页面
     */
    @Override
    public List<BmsSightseeingBusApplicant> getListByUserId() {
        //根据token获取用户基本信息
        UmsMember umsMember = MemberUtils.getUser();
        BmsSightseeingBusApplicant query=new BmsSightseeingBusApplicant();
        query.setUserId(umsMember.getId());
        return bmsSightseeingBusApplicantMapper.getListByUserId(query);
    }


    @Override
    public BmsSightseeingBusApplicant getSuccessBusDataByUserId(BmsSightseeingBusApplicant bmsSightseeingBusApplicant){
        //根据token获取用户基本信息
        UmsMember umsMember = MemberUtils.getUser();
        bmsSightseeingBusApplicant.setUserId(umsMember.getId());
        BmsSightseeingBusApplicant successBusDataByUserId = bmsSightseeingBusApplicantMapper.getSuccessBusDataByUserId(bmsSightseeingBusApplicant);
        successBusDataByUserId.setUserName(umsMember.getNickname());
        return successBusDataByUserId;
    }
}
